package TreeSeries;

import java.util.LinkedList;
import java.util.Queue;

public class IsSymmetricSolution_101 {
    public boolean isSymmetric_DFS(TreeNode root) {
        return compare(root.left, root.right);
    }

    public boolean isSymmetric_BFS(TreeNode root) {
        if (root == null) {
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();


        queue.offer(root.left);
        queue.offer(root.right);
        while (!queue.isEmpty()) {
            TreeNode tempLeft = queue.poll();
            TreeNode tempRight = queue.poll();

            if (tempLeft == null && tempRight == null) {
                continue;
            }
            if (tempLeft == null || tempRight == null || tempLeft.val != tempRight.val) {
                return false;
            }

            queue.offer(tempLeft.left);
            queue.offer(tempRight.right);
            queue.offer(tempLeft.right);
            queue.offer(tempRight.left);

        }
        return true;
    }


    // DFS
    public boolean compare(TreeNode left, TreeNode right) {
        if (left == null && right != null) {
            return false;
        }

        if (left != null && right == null) {
            return false;
        }

        if (left == null && right == null) {
            return true;
        }

        if (left.val != right.val) {
            return false;
        }

        boolean compareOut = compare(left.left, right.right);
        boolean compareIn = compare(left.right, right.left);
        return compareIn && compareOut;
    }
}
